﻿@namespace AntSK.Pages.ChatPage.Components
@using AntSK.Domain.Repositories
@using AntSK.Models
@using Microsoft.AspNetCore.Components.Web.Virtualization
@layout OpenLayout
@inherits AntDomComponentBase


<div id="chat">
    @if (ShowTitle)
    {
        <PageHeader Class="site-page-header" Title="@app.Name" Subtitle="@app.Describe" />
    }
    <div id="scrollDiv" class="chat-scroll">
        <Virtualize Items="@(MessageList.OrderBy(o => o.CreateTime).ToList())" Context="item">
            @if (item.IsSend)
            {
                <GridRow Class="message-row" Gutter="(8,8)">
                    <GridCol Span="23">
                        <div class="message-shell sent">
                            <div class="chat-bubble sent">
                                <Popover Title="@item.CreateTime.ToString()">
                                    <Unbound>
                                        <Flex Vertical RefBack="context" Class="bubble-body">
                                            @if (item.FileName != null)
                                            {
                                                <p class="message-file">
                                                    <Upload DefaultFileList="[new(){ FileName= item.FileName }]" />
                                                </p>
                                            }
                                            <p class="bubble-text">@(item.Context)</p>
                                        </Flex>
                                    </Unbound>
                                </Popover>
                            </div>
                            <div class="message-meta meta-right">
                                <span>@item.CreateTime.ToString("HH:mm")</span>
                                <Icon Class="meta-action" Type="copy" Theme="outline" OnClick="async () =>await OnCopyAsync(item)" />
                            </div>
                        </div>
                    </GridCol>
                    <GridCol Span="1" Class="avatar-col">
                        <Image Class="avatar" Width="32px" Height="32px" Src="./assets/KDpgvguMpGfqaHPjicRK.svg" Preview="false" />
                    </GridCol>
                </GridRow>
            }
            else
            {
                <GridRow Class="message-row" Gutter="(8,8)">
                    <GridCol Span="1" Class="avatar-col">
                        <Image Class="avatar" Width="32px" Height="32px" Src="./assets/method-draw-image.svg" Preview="false" />
                    </GridCol>
                    <GridCol Span="23">
                        <div class="message-shell received">
                            <div class="chat-bubble received">
                                @((MarkupString)(item.Context))
                            </div>
                            <div class="message-meta meta-left">
                                <span>@item.CreateTime.ToString("HH:mm")</span>
                            </div>
                        </div>
                    </GridCol>
                </GridRow>
            }
        </Virtualize>
    </div>

    @if (fileList.Count > 0)
    {
        <Flex Vertical>
            <Upload DefaultFileList="fileList" OnRemove="HandleFileRemove" />
        </Flex>
    }
    <Flex Class="input-bar" Justify="end" Align="center">
        <div class="input-bar__field">
            <AntDesign.Input @bind-Value="@(_messageInput)" DebounceMilliseconds="@(-1)" Placeholder="输入消息回车发送" OnPressEnter="@(async () => await OnSendAsync())" Disabled="@Sendding"></AntDesign.Input>
        </div>
        @if (app.EmbeddingModelID != null)
        {
            <Upload Action="@("api/File/UploadFile")"
                    Name="file"
                    Accept="*/*"
                    ShowUploadList="false"
                    BeforeUpload="_kMService.BeforeUpload"
                    OnSingleCompleted="OnSingleCompleted">
                <Button Icon="@IconType.Outline.Upload" Type="@(ButtonType.Link)" Disabled="@Sendding" />
            </Upload>
        }
        <Button Icon="clear" Type="@(ButtonType.Link)" OnClick="@(async () => await OnClearAsync())" Disabled="@Sendding"></Button>
        <Button Icon="send" Type="@(ButtonType.Primary)" OnClick="@(async () => await OnSendAsync())" Disabled="@Sendding"></Button>
    </Flex>
</div>

<style>
    :root {
        --surface-base: #ffffff;
        --surface-elevated: #f5f7fb;
        --border-subtle: rgba(15, 23, 42, 0.07);
        --shadow-soft: 0 16px 28px rgba(15, 23, 42, 0.08);
        --primary-color: var(--ant-primary-color, #1677ff);
        --primary-soft: rgba(22, 119, 255, 0.12);
        --bubble-recv: #ffffff;
        --bubble-border-sent: rgba(22, 119, 255, 0.25);
        --text-primary: #1f2329;
        --text-secondary: #7a808a;
    }

    @@supports (color: color-mix(in srgb, white 50%, black 50%)) {
        :root {
            --bubble-sent: color-mix(in srgb, var(--primary-color) 28%, white);
        }
    }

    #chat {
        position: relative;
        isolation: isolate;
        z-index: 0;
        display: flex;
        flex-direction: column;
        gap: 8px;
        flex: 1;
        height: 100%;
        width: 100%;
        min-height: min(65vh, 100%);
        max-height: 100%;
        background: var(--surface-base);
        border-radius: 16px 16px 16px 16px;
        padding: 12px 12px 0 12px;
        box-sizing: border-box;
        border: 1px solid var(--border-subtle);
    }

    #chat::after {
        content: "";
        position: absolute;
        left: 32px;
        right: 32px;
        bottom: -18px;
        height: 40px;
        background: rgba(15, 23, 42, 0.22);
        filter: blur(24px);
        border-radius: 50%;
        pointer-events: none;
        z-index: -1;
    }

    .chat-scroll {
        flex: 1;
        width: 100%;
        min-height: 0;
        max-height: 100%;
        overflow-y: auto;
        overflow-x: hidden;
        padding: 12px 16px 20px;
        background: var(--surface-base);
        border-radius: 16px;
        box-sizing: border-box;
        scroll-behavior: smooth;
        scrollbar-gutter: stable both-edges;
    }

    .chat-scroll::-webkit-scrollbar {
        width: 6px;
    }

    .chat-scroll::-webkit-scrollbar-thumb {
        background: rgba(22, 119, 255, 0.24);
        border-radius: 8px;
    }

    .chat-scroll::-webkit-scrollbar-track {
        background: rgba(15, 23, 42, 0.05);
        border-radius: 8px;
    }

    .message-row {
        margin-bottom: 4px;
    }

    .message-shell {
        display: flex;
        flex-direction: column;
        gap: 6px;
    }

    .message-shell.sent {
        align-items: flex-end;
    }

    .message-shell.received {
        align-items: flex-start;
    }

    .chat-bubble {
        display: inline-flex;
        flex-direction: column;
        gap: 6px;
        padding: 14px 16px;
        border-radius: 16px;
        max-width: 78%;
        position: relative;
        box-shadow: 0 8px 20px rgba(15, 23, 42, 0.08);
        border: none;
        word-break: break-word;
        line-height: 1.65;
        animation: pop .2s ease-out;
        transition: transform .2s ease, box-shadow .2s ease;
    }

    .chat-bubble:hover {
        transform: translateY(-1px);
        box-shadow: 0 14px 30px rgba(15, 23, 42, 0.12);
    }

    .chat-bubble .bubble-text {
        margin: 0;
        white-space: pre-wrap;
        color: var(--text-primary);
    }

    .chat-bubble .bubble-body {
        gap: 10px;
    }

    .chat-bubble.received {
        background: var(--bubble-recv);
        box-shadow: 0 8px 18px rgba(15, 23, 42, 0.06), inset 0 0 0 1px rgba(15, 23, 42, 0.05);
    }

    .chat-bubble.sent {
    background: var(--bubble-sent);
        color: #0c1a33;
        box-shadow: 0 12px 26px rgba(22, 119, 255, 0.18);
    }

    .chat-bubble.sent .bubble-text {
        color: #102347;
    }

    /* bubble tails */
    .chat-bubble.received::after,
    .chat-bubble.sent::after {
        content: "";
        position: absolute;
        top: 16px;
        bottom: auto;
        width: 18px;
        height: 18px;
        transform: none;
        border-radius: 0;
        box-shadow: none !important;
        filter: none !important;
    }

    .chat-bubble.received::after {
        left: -14px;
        background: var(--bubble-recv);
        clip-path: polygon(100% 0, 0 0, 100% 100%);
    }

    .chat-bubble.sent::after {
        right: -14px;
        background: var(--bubble-sent);
        clip-path: polygon(0 0, 100% 0, 0 100%);
    }

    .message-meta {
        font-size: 12px;
        color: var(--text-secondary);
        display: flex;
        align-items: center;
        gap: 8px;
        margin: 0;
    }

    .meta-right {
        justify-content: flex-end;
    }

    .meta-left {
        justify-content: flex-start;
        padding-left: 4px;
    }

    .meta-action {
        color: var(--text-secondary);
        cursor: pointer;
        transition: color .2s ease;
    }

    .meta-action:hover {
        color: var(--primary-color);
    }

    .avatar-col {
        display: flex;
        justify-content: center;
    }

    .avatar {
        width: 32px;
        height: 32px;
        aspect-ratio: 1 / 1;
        object-fit: cover;
        border-radius: 50%;
        box-shadow: 0 0 0 1px rgba(15, 23, 42, 0.12) inset, 0 6px 16px rgba(15, 23, 42, 0.12);
    }

    .input-bar {
        display: flex;
        align-items: center;
        gap: 8px;
        padding: 12px;
        border-top: 1px solid rgba(15, 23, 42, 0.06);
        background: var(--surface-base);
        position: sticky;
        bottom: 0;
        box-shadow: 0 -12px 24px rgba(15, 23, 42, 0.06);
        border-radius: 16px;
    }

    .input-bar .ant-input {
        border-radius: 999px;
        padding: 10px 16px;
    }

    .input-bar__field {
        flex: 1;
        min-width: 0;
        display: flex;
        align-items: center;
        padding: 2px;
        border-radius: 999px;
        background: var(--surface-elevated);
        border: 1px solid var(--border-subtle);
    }

    .input-bar__field .ant-input {
        width: 100%;
        border: none;
        background: transparent;
        box-shadow: none;
        height: 100%;
    }

    /* markdown & code inside bubbles */
    .chat-bubble pre {
        background: #0b1021;
        color: #e6e6e6;
        padding: 12px;
        border-radius: 12px;
        overflow: auto;
        box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.06);
    }

    .chat-bubble code {
        background: rgba(27, 31, 35, 0.08);
        padding: .15rem .35rem;
        border-radius: 6px;
    }

    .chat-bubble pre code {
        background: transparent;
        padding: 0;
    }

    .chat-bubble table {
        width: 100%;
        border-collapse: collapse;
    }

    .chat-bubble table td,
    .chat-bubble table th {
        border: 1px solid rgba(15, 23, 42, 0.08);
        padding: 6px 8px;
    }

    .think {
        color: rgba(22, 119, 255, 0.9);
        font-style: italic;
        text-align: left !important;
        display: block;
        margin-top: 5px;
        margin-left: 8px;
        padding-left: 10px;
        border-left: 3px solid rgba(22, 119, 255, 0.4);
    }

    .message-file {
        margin: 0;
        border-radius: 10px;
        overflow: hidden;
    }

    .message-file .ant-upload-list-item {
        background: rgba(22, 119, 255, 0.08);
        border: none !important;
    }

    @@media (max-width: 992px) {
        #chat {
            border-radius: 12px;
            padding: 10px 10px 0;
        }

        .chat-bubble {
            max-width: 88%;
        }

        .input-bar {
            flex-wrap: wrap;
        }
    }

    @@media (prefers-color-scheme: dark) {
        :root {
            --surface-base: #111827;
            --surface-elevated: #1f2937;
            --border-subtle: rgba(148, 163, 184, 0.16);
            --shadow-soft: 0 18px 34px rgba(0, 0, 0, 0.45);
            --text-primary: #f1f5f9;
            --text-secondary: #94a3b8;
            --bubble-recv: #1f2937;
        }

        #chat {
            background: var(--surface-elevated);
            box-shadow: 0 28px 48px -24px rgba(0, 0, 0, 0.68);
        }

        #chat::after {
            background: rgba(15, 23, 42, 0.55);
        }

        .chat-scroll {
            background: transparent;
        }

        .chat-bubble {
            border: 1px solid rgba(148, 163, 184, 0.16);
        }

        .chat-bubble.received::after {
            background: var(--bubble-recv);
            border: none;
        }

        .message-file .ant-upload-list-item {
            background: rgba(22, 119, 255, 0.16);
        }

        .input-bar {
            background: var(--surface-elevated);
        }
    }

    @@keyframes pop {
        from { transform: translateY(6px); opacity: .55; }
        to { transform: translateY(0); opacity: 1; }
    }
</style>

@code {

}
